package com.mall.ums.exception;

import com.mall.model.domain.vo.ErrorResult;
import com.mall.utils.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

@ControllerAdvice
@ResponseBody
@Slf4j
public class ExceptionAdvice {

    @ExceptionHandler(BusinessException.class)
    public R<String> businessException(BusinessException be) {
        log.error(be.getMessage());
        return R.error(be.getMessage());
    }

    @ExceptionHandler(Exception.class)
    public ResponseEntity exception(Exception e) {
        e.printStackTrace();
        ErrorResult errorResult = new ErrorResult();
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(ErrorResult.error());
    }
}
